﻿// <copyright file="QuaternionConverter.cs" company="Axelerate Solutions S.A.">
// Copyright (c) Axelerate Solutions S.A.  All rights reserved.
// </copyright>

namespace Axelerate.Silverlight3D.Media.Media3D
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;

    /// <summary>
    /// File name: QuaternionConverter.cs
    /// Company: Axelerate Solutions S.A.
    /// Created By: Daniel Rojas
    /// Created Date: 10/10/2008 10:03:00 h.
    /// Description: Allow Convert a Quaterion to and from string
    ///   <Modifications>
    ///   </Modifications>
    /// </summary>
    public class QuaternionConverter : TypeConverter
    {
        /// <summary>
        /// Gets a value that indicates whether this type converter can convert from a given type.
        /// </summary>
        /// <param name="context">ITypeDescriptorContext for this call.</param>
        /// <param name="sourceType">Type being queried for support.</param>
        /// <returns>True if this converter can convert from the provided type, false otherwise.</returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType.Equals(typeof(string)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Gets a value that indicates whether this type converter can convert to the given type.
        /// </summary>
        /// <param name="context">ITypeDescriptorContext for this call.</param>
        /// <param name="destinationType">Type being queried for support.</param>
        /// <returns>True if this converter can convert to the provided type, false otherwise.</returns>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType.Equals(typeof(string)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Converts the specified object to a Quaternion.
        /// </summary>
        /// <param name="context">ITypeDescriptorContext for this call.</param>
        /// <param name="culture">CultureInfo to be respected when converting.</param>
        /// <param name="value">Object to convert to an instance of Quaternion.</param>
        /// <returns>Quaternion that was constructed.</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                return Quaternion.Parse((string)value);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Attempts to convert an instance of Quaternion to the given type.
        /// </summary>
        /// <param name="context">The ITypeDescriptorContext for this call.</param>
        /// <param name="culture">The CultureInfo which is respected when converting.</param>
        /// <param name="value">The object to convert to an instance of destinationType.</param>
        /// <param name="destinationType">The type to which the Quaternion instance will be converted.</param>
        /// <returns>Object that was constructed.</returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType.Equals(typeof(string)) && value is Quaternion)
            {
                return ((Quaternion)value).ToString();
            }
            else
            {
                return null;
            }
        }
    }
}
